local handler = require "handler"
local util = require "util"
local reason = require "reason"
local error_code = require "error_code"
local constant = require "constant"

local table_insert = table.insert
local table_sort = table.sort
local math_floor = math.floor
local notify_money_changed = handler.notify_money_changed
local add_awards = handler.add_awards
local tostring = tostring


local MAX_SIGNING_DAYS = 8
local daily_handler = {}

local function check_signing_info(player)
    local user_data = player.user_data
    local sign_info = user_data.sign_info

    local curr_time = util.get_now_time()
    local last_time = sign_info.last_time

    if util.is_same_day(curr_time,last_time) then
        return
    end

    if util.is_same_day(curr_time,last_time + 86400) then
        --昨天签到过并且是最后一天，那么回退一天，以便客户端显示
        if sign_info.days >= MAX_SIGNING_DAYS then
            sign_info.days = MAX_SIGNING_DAYS - 1
        end
        return
    end

    --上一次签到是离今天已经超过一天了,则清除签到的记录
    if sign_info.days ~= 0 then
        sign_info.days = 0
    end
end

function daily_handler.REQ_SIGNING_PANEL(msg)
    -- local player = player
    -- check_signing_info(player)

    -- local uid = player.uid
    -- local user_data = player.user_data
    -- local sign_info = user_data.sign_info

    -- local curr_time = util.get_now_time()

    -- --今日是否已经签到
    -- local signed = false
    -- if util.is_same_day(curr_time,sign_info.last_time) then
    --     signed = true
    -- end

    -- local signing_cfg = global_configs.signing

    -- local day_award_list = {}

    -- --读取配置
    -- for k,v in ipairs(signing_cfg) do
    --     local award_list = {}
    --     for i=1,#v.awards_list do
    --         local one_award = {}
    --         one_award.id = v.awards_list[i].id
    --         one_award.count = v.awards_list[i].count
    --         table_insert(award_list,one_award)
    --     end
    --     table_insert(day_award_list,{
    --         day = k,
    --         award_list = award_list,
    --     })
    -- end

    -- local rsp = {
    --     signed = signed,
    --     signing_days = sign_info.days,
    --     day_award_list = day_award_list,
    -- }

    -- send_to_gateway('daily.RSP_SIGNING_PANEL',rsp)
    return true
end

function daily_handler.REQ_SIGN(msg)
    -- local player = player
    -- check_signing_info(player)

    -- local uid = player.uid
    -- local user_data = player.user_data

    -- local sign_info = user_data.sign_info

    -- local curr_time = util.get_now_time()
    -- local last_time = sign_info.last_time

    -- --今日是否已经签到
    -- if util.is_same_day(curr_time,last_time) then
    --     dbglog("today you are already signin",last_time)
    --     send_to_gateway('daily.RSP_SIGN',{result = error_code.TODAY_IS_ALREADY_SIGN})
    --     return
    -- end

    -- --连续签到天数+1
    -- local days = sign_info.days
    -- if days < MAX_SIGNING_DAYS then
    --     days = days + 1
    -- end

    -- sign_info.days = days
    -- sign_info.last_time = curr_time
 
    -- local signing_cfg = global_configs.signing
    -- local day_cfg = signing_cfg[days]
    -- if not day_cfg then
    --     errlog(uid,'no such day config',days)
    --     send_to_gateway('daily.RSP_SIGN',{result = -120})
    --     return
    -- end

    -- local rsp_award_list = {}
    -- for _,award_info in ipairs(day_cfg.awards_list) do
    --     local one_award = {}
    --     one_award.id = award_info.id
    --     one_award.count = award_info.count
    --     table_insert(rsp_award_list,one_award)
    -- end

    -- -- 发放奖励
    -- local chged
    -- if #rsp_award_list > 0 then
    --     chged = add_awards(uid,rsp_award_list,reason.SIGN_IN)
    --     if not chged then
    --         errlog(uid,"failed to add_awards",tostring_r(rsp_award_list))
    --         send_to_gateway('daily.RSP_SIGN',{result = -3})
    --         return
    --     end
    -- end

    -- local rsp = {
    --     signing_days = days,
    --     award_list = rsp_award_list,
    -- }
    -- send_to_gateway('daily.RSP_SIGN',rsp)

    -- if chged then
    --     notify_money_changed(uid,chged)
    -- end

    return true
end

------------------------任务模块----------------------------
local function notify_curr_task()
    local rsp_cycle_list = get_task_cycle_list()
    send_to_gateway('daily.NTF_CURR_TASK',{cycle_list = rsp_cycle_list})
    return true
end

daily_handler.notify_curr_task = notify_curr_task

---------------------------新版任务模块--------------------------
function daily_handler.REQ_TASK_LIST(msg)
    local rsp_task_list = player:make_task_list()
    send_to_gateway('daily.RSP_TASK_LIST',{task_list = rsp_task_list})
    return true
end

function daily_handler.REQ_TAKE_TASK_AWARD(msg)
    local uid = player.uid
    local task_id = msg.task_id
    
    local ok,ret = player:take_task_award(task_id)
    if not ok then
        send_to_gateway('daily.RSP_TAKE_TASK_AWARD',{result = ret})
        return
    end

    --发奖励
    local chged = add_awards(uid,ret,reason.TAKE_TASK_AWARD)
    if not chged then
        errlog(uid,"failed to add_awards")
        send_to_gateway('daily.RSP_TAKE_TASK_AWARD',{result = -3})
        return
    end

    send_to_gateway('daily.RSP_TAKE_TASK_AWARD',{task_id = task_id})
    
    if chged then
        notify_money_changed(uid,chged)
    end

    return true
end

function daily_handler.REQ_REPORT_SHARING(msg)
    local uid = player.uid
    player:update_share_progress(1)

    --检查进度是否有更新，有的话则通知变化
    local task_type = constant.TASK_TYPE_DEF.TASK_TYPE_SHARE
    if player:check_task_process(task_type) then
        send_to_gateway('hall.NTF_RED_POINTS',{
            module_list = {constant.MODULE_DEF.TASK}
        })
    end

    send_to_gateway('daily.RSP_REPORT_SHARING',{})

    player:billlog('share',{
        channel = msg.channel,
        share_way = msg.share_way,
        reason = msg.reason
    })
    
    return true
end
--------------------------------------------------------
handler.register_module_handlers('daily',daily_handler)